{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9adb78de-aea8-43a3-9599-f7a6a52c9a1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K近邻预测结果: 数字 4\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAGZCAYAAABmNy2oAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAOcklEQVR4nO3cXWzddR3H8e+Zp7RhD2DGpExCRxoYVYxkPlXjAqgRNUyCGKJo6CYsXADqMGIMJG7iA4mLwQu90jExyxQZgWQGF4zjalW40ASi8WEyFjQsPjAzlcgKPy/MPqFsq2vtdrbxeiW72P/8z/98W2jf+52HX6e11goAqmpOrwcA4PghCgCEKAAQogBAiAIAIQoAhCgAEKIAQIgCACEKzIqNGzdWp9PJn263W2effXatWrWq/vjHPx6TGZYsWVIrV67M3x955JHqdDr1yCOPTOs6O3bsqLVr19bevXtndb6qqpUrV9aSJUtm5Vp79uyphQsXVqfTqfvuu29WrgmiwKy6++67a3x8vB5++OFavXp1bd68uZYvX17//Oc/j/ksy5Ytq/Hx8Vq2bNm07rdjx45at27dUYnCbLrxxhtrYGCg12NwkhEFZtWFF15Yo6Ojdemll9YXvvCFuvXWW+vJJ5+sBx544LD3+de//nVUZlmwYEGNjo7WggULjsr1e2nLli21bdu2+upXv9rrUTjJiAJH1ejoaFVVPfXUU1X136dP5s2bV48//ni9973vrfnz59e73/3uqqp6/vnn60tf+lJdcMEF1d/fX4sWLapVq1bVn//850nX3L9/f9166601ODhYp556ar3zne+sRx999KDHPtzTRz//+c9rxYoVtXDhwhoYGKjh4eH69Kc/XVVVa9eurc9+9rNVVXXuuefm6bCXXuMHP/hBvf3tb6+5c+fWvHnz6rLLLqtf/OIXBz3+xo0ba+nSpdXf318jIyN1zz33zOh7+HJ/+9vf6sYbb6wvf/nLdc4558zKNeEAUeCo+v3vf19VVYsWLcqx559/vj74wQ/Wu971rnrwwQdr3bp19eKLL9YVV1xRd955Z11zzTX1ox/9qO688856+OGH65JLLqnnnnsu91+9enWtX7++rr322nrwwQfrqquuqg996EP17LPP/s95tm3bVsuXL6/du3fX17/+9XrooYfq9ttvrz179lRV1fXXX18333xzVVXdf//9NT4+PukpqK985Sv10Y9+tF73utfVvffeW9/73vdq3759tXz58vrVr36Vx9m4cWOtWrWqRkZGasuWLXX77bfXHXfcUT/96U8PmmnlypXV6XRq165dR/Q9/eQnP1nnnntu3XTTTUd0PkxLg1lw9913t6pqP/vZz9r+/fvbvn372tatW9uiRYva/Pnz2zPPPNNaa21sbKxVVduwYcOk+2/evLlVVduyZcuk44899lirqvatb32rtdbar3/961ZVbc2aNZPO27RpU6uqNjY2lmPbt29vVdW2b9+eY8PDw214eLg999xzh/1avva1r7Wqak8++eSk47t3727dbrfdfPPNk47v27evDQ4Otquvvrq11toLL7zQFi9e3JYtW9ZefPHFnLdr167W19fXhoaGJt3/E5/4RHvVq17Vdu3addiZDti6dWvr6+trjz/++KSv8Yc//OH/vC8cCSsFZtXo6Gj19fXV/Pnz6/LLL6/BwcF66KGH6swzz5x03lVXXTXp71u3bq3TTz+9VqxYURMTE/lz0UUX1eDgYJ6+2b59e1VVfexjH5t0/6uvvrq63e6Us/32t7+tnTt31nXXXTejF2i3bdtWExMTde21106acWBgoC6++OLM+Jvf/Kb+9Kc/1TXXXFOdTif3Hxoaqne84x0HXfc73/lOTUxM1NDQ0JSP//e//71uuOGG+tznPlcXXnjhtOeHIzH1TxFM0z333FMjIyPV7XbrzDPPrLPOOuugc0499dSDXvzds2dP7d27t0455ZRDXvcvf/lLVVX99a9/raqqwcHBSbd3u91auHDhlLMdeG3i7LPPPrIv5mUOPMX0lre85ZC3z5kzZ8oZDxw70qeJXu62226rvr6+uummm/LOqH/84x9V9d8X6/fu3VunnXbapBDBdIkCs2pkZKTe/OY3T3nOoX5pnXHGGbVw4cL68Y9/fMj7zJ8/v6oqv/ifeeaZeu1rX5vbJyYm8sv4cA68rvH0009Ped7hnHHGGVVVdd999035r/qXzvhyhzp2pJ544onatWvXIWMzNjZWVVXPPvtsnX766TN+DBAFjguXX355ff/7368XXnih3va2tx32vEsuuaSqqjZt2lRvetObcvzee++tiYmJKR/j/PPPr+Hh4dqwYUPdcsst1d/ff8jzDhx/6YvbVVWXXXZZdbvd2rlz50FPf73U0qVL66yzzqrNmzfXLbfckgg+9dRTtWPHjlq8ePGUcx7OXXfdddBnJ375y1/WmjVrau3atXXxxRfXvHnzZnRtOEAUOC585CMfqU2bNtUHPvCB+tSnPlVvfetbq6+vr55++unavn17XXHFFXXllVfWyMhIffzjH6+77rqr+vr66j3veU898cQTtX79+iP6PMI3v/nNWrFiRY2OjtaaNWvqnHPOqd27d9e2bdtq06ZNVVX1hje8oaqqvvGNb9TY2Fj19fXV0qVLa8mSJfXFL36xbrvttvrDH/5Q73vf++rVr3517dmzpx599NGaO3durVu3rubMmVN33HFHXX/99XXllVfW6tWra+/evbV27dpD/iv/uuuuq+9+97u1c+fOKVcgF1100WFve/3rX59gwv+l1690c3I48O6jxx57bMrzxsbG2ty5cw952/79+9v69evbG9/4xjYwMNDmzZvXLrjggnbDDTe03/3udznv3//+d/vMZz7TXvOa17SBgYE2OjraxsfH29DQ0P9891FrrY2Pj7f3v//97bTTTmv9/f1teHj4oHczff7zn2+LFy9uc+bMOegaDzzwQLv00kvbggULWn9/fxsaGmof/vCH209+8pNJ1/j2t7/dzjvvvHbKKae0888/v23YsKGNjY0d9O6jA+/Ievm7nY6Edx8x2zqttdbTKgFw3PCWVABCFAAIUQAgRAGAEAUAQhQAiCP+8Jr9VABObEfyCQQrBQBCFAAIUQAgRAGAEAUAQhQACFEAIEQBgBAFAEIUAAhRACBEAYAQBQBCFAAIUQAgRAGAEAUAQhQACFEAIEQBgBAFAEIUAAhRACBEAYAQ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KIAQIgCAGGbC2ad7TxOHkfzv2Wn0zlq12bmrBQACFEAIEQBgBAFAEIUAAhRACBEAYAQBQBCFAAIUQAgRAGAsPfR/8EePzBz0/35sVfSsWGlAECIAgAhCgCEKAAQogBAiAIAIQoAhCgAEKIAQIgCACEKAIQoABCiAECIAgAhCgCEKAAQogBAiAIAIQoAhCgAEKIAQIgCANHt9QAnsk6nM63zW2tHaRI48Uz354djw0oBgBAFAEIUAAhRACBEAYAQBQBCFAAIUQAgRAGAEAUAwjYXx5CP9Z9cXgnblvh/9pXHSgGAEAUAQhQACFEAIEQBgBAFAEIUAAhRACBEAYAQBQBCFAAIUQAgRAGAEAUAQhQACFEAIEQBgBAFAEIUAAhRACBEAYAQBQBCFAAIUQAgRAGAEAUAQhQACFEAIEQBgBAFAEIUAAhRACBEAYAQBQBCFAAIUQAgRAGAEAUAQhQACFEAIEQBgBAFAEIUAAhRACBEAYAQBQCi2+sBgGOr0+n0egSOY1YKAIQoABCiAECIAgAhCgCEKAAQogBAiAIAIQoAhCgAELa5gBk6mttFtNaOizl45bFSACBEAYAQBQBCFAAIUQAgRAGAEAUAQhQACFEAIEQBgBAFAMLeR3Acsp8RvWKlAECIAgAhCgCEKAAQogBAiAIAIQoAhCgAEKIAQIgCACEKAIQoABCiAECIAgAhCgCEKAAQogBAiAIAIQoAhCgAEKIAQIgCANE90hNba0dzDgCOA1YKAIQoABCiAECIAgAhCgCEKAAQogBAiAIAIQoAxH8Anb2AnawwHbgAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#1.K近邻算法\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "df = pd.read_excel('D:/手写字体识别.xlsx')\n",
    "X = df.drop(columns='对应数字')\n",
    "y = df['对应数字']\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "# 使用K近邻算法训练模型\n",
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "knn.fit(X_train, y_train)\n",
    "# 读取并预处理图片\n",
    "img = Image.open('D:/数字4.png')\n",
    "img = img.resize((32, 32))  # 调整大小为32x32\n",
    "img = img.convert('L')      \n",
    "# 二值化处理\n",
    "img_binary = img.point(lambda x: 0 if x > 128 else 1)\n",
    "arr = np.array(img_binary)\n",
    "# 将二维数组转换为一维数组\n",
    "arr_flat = arr.reshape(1, -1)\n",
    "\n",
    "predicted_number = knn.predict(arr_flat)[0]\n",
    "print(f\"K近邻预测结果: 数字 {predicted_number}\")\n",
    "\n",
    "plt.imshow(arr, cmap='gray')\n",
    "plt.title(f'Predicted: {predicted_number}')\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7134fbdb-f719-4252-baa8-0181c21d5d56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVM预测结果: 数字 4\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAGZCAYAAABmNy2oAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAR10lEQVR4nO3cf6zVdf3A8deBe+HeC5YMDASm/KgGEioFU0C44iz8gRBlvyCFIrecCBgWUaZZGsRGRCSyTLiOSNhiEBIsN0NyzRib6MJkpuXaChAoBUXiwv18/yheX68X5PLzCvfx2NjgnPf5nNe5/Hjy/txzPqWiKIoAgIho0dQDAPDeIQoAJFEAIIkCAEkUAEiiAEASBQCSKACQRAGAJArN1IYNG2L06NFxwQUXROvWraNjx44xcODAmDp1akRE7NixI1q1ahWf//znj3iM3bt3R1VVVYwcOTIiImpqaqJUKkWpVIonn3yywfqiKOKDH/xglEqluPLKK48645VXXpnHK5VKUVlZGZdcckn8+Mc/jrq6uuN63cfiySefbPBaxo8fH926dTvmY82fPz9qampO2mxvVyqV4rvf/e5JOdbPf/7zKJVK0bZt25NyPM48otAM/eY3v4lBgwbF7t27Y9asWfH444/H3LlzY/DgwbFs2bKIiDjvvPNi5MiRsXLlyvj3v/992OMsXbo03nrrrZgwYUK9288555x4+OGHG6xfv359vPzyy3HOOec0etYePXrE008/HU8//XQsW7YsunTpEnfccUdMnz79GF7xyfOd73wnVqxYccyPO5VROFn+8Y9/xJ133hmdO3du6lFoSgXNztChQ4uePXsWtbW1De47ePBg/nzNmjVFRBTz5s077HEuu+yyomPHjnmcRYsWFRFRfOUrXykqKyuL119/vd76L37xi8XAgQOLPn36FNXV1Ueds7q6uujTp0+92/bv31/06NGjqKqqKvbv33/Yx9XV1RV79+496vGPZt26dUVEFOvWrTvhYzX2NR+PiCjuueeeEz7OiBEjihtuuKEYN25c0aZNmxMfjDOSnUIztGvXrujQoUOUlZU1uK9Fi///IzF8+PDo2rVrLFq0qMG6F154ITZs2BA333xzg+N84QtfiIiIRx99NG97/fXXY/ny5fHlL3/5hGYvLy+Pj33sY7F3797YsWNHRPz39MnEiRNjwYIF0bt372jdunU88sgjERHxl7/8JcaMGRMf+MAHonXr1tG7d+944IEHGhx3y5Ytcc0110RVVVV06NAhvvrVr8aePXsarDvc6aO6urqYN29eXHrppVFZWRnnnntuXH755bFq1aqIiOjWrVs8//zzsX79+jwV9vZj7N69O+68887o3r17tGrVKrp06RJTpkyJN998s97z7N69O2655ZZo3759tG3bNq655pp48cUXT+TLmX7xi1/E+vXrY/78+SfleJy5RKEZGjhwYGzYsCEmTZoUGzZsiNra2sOua9GiRYwfPz6eeeaZeO655+rddygUh/tH/n3ve1/ceOONsXDhwrzt0UcfjRYtWs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CJAgDJZS7gOJ3Ky0UURfGemIPmx04BgCQKACRRACCJAgBJFABIogBAEgUAkigAkEQBgCQKACRRACC59hG8B7meEU3FTgGAJAoAJFEAIIkCAEkUAEiiAEASBQCSKACQRAGAJAoAJFEAIIkCAEkUAEiiAEASBQCSKACQRAGAJAoAJFEAIIkCAEkUAEiiAEAqa+zCoihO5RwAvAfYKQCQRAGAJAoAJFEAIIkCAEkUAEiiAEASBQCSKACQ/g9QnKDqpb8QjgAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#2.SVM算法\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "df = pd.read_excel('D:/手写字体识别.xlsx')\n",
    "X = df.drop(columns='对应数字')\n",
    "y = df['对应数字']\n",
    "# 划分训练测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "# SVM模型训练\n",
    "svm_model = SVC(kernel='rbf', C=1.0, gamma='scale', probability=True)\n",
    "svm_model.fit(X_train, y_train)\n",
    "# 预测图片\n",
    "img = Image.open('D:/数字4.png')\n",
    "img = img.resize((32, 32)).convert('L')\n",
    "img_binary = img.point(lambda x: 0 if x > 128 else 1)\n",
    "arr = np.array(img_binary).reshape(1, -1)\n",
    "predicted_number = svm_model.predict(arr)[0]\n",
    "print(f\"SVM预测结果: 数字 {predicted_number}\")\n",
    "plt.imshow(arr.reshape(32, 32), cmap='gray')\n",
    "plt.title(f'SVM Predicted: {predicted_number}')\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "46f51a0f-1699-402c-998c-4dca71da6b28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林预测结果: 数字 4\n"
     ]
    },
    {
     "data": {
      "image/png": "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MAQBQCAIQoAAEMUAACGKAAADFEAABiiAAAwRAEAYIgCAMAQBQCAIQoAAEMUAACGKAAADFEAABiiAAAwRAEAYIgCAMAQBQCAIQoAAEMUAACGKAAADFEAABiiAAAwRAEAYIgCAMAQBQCAIQoAAEMUAACGKAAADFEAABiiAAAwRAEAYIgCAMAQBQCAcYV7AACuL8dxwj0E3MDYUwAAGKIAADBEAQBgiAIAwBAFAIAhCgAAQxQAAIYoAAAMUQAAGKIAADBc5gJoomt5uQhVvSHGge8f9hQAAIYoAAAMUQAAGKIAADBEAQBgiAIAwBAFAIAhCgAAQxQAAIYoAAAMUQAAGK59BNyAuJ4RwoU9BQCAIQoAAEMUAACGKAAADFEAABiiAAAwRAEAYIgCAMAQBQCAIQoAAEMUAACGKAAADFEAABiiAAAwRAEAYIgCAMAQBQCAIQoAAEMUAACGKAAADFEAABiiAAAwrqudUVWv5TgAADcA9hQAAIYoAAAMUQAAGKIAADBEAQBgiAIAwBAFAIAhCgAAQxQAAOb/ABAyHvmaBUwKAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#3.随机森林算法\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "df = pd.read_excel('D:/手写字体识别.xlsx')\n",
    "X = df.drop(columns='对应数字')\n",
    "y = df['对应数字']\n",
    "# 划分训练测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "# 随机森林模型训练\n",
    "rf_model = RandomForestClassifier(n_estimators=100, random_state=123)\n",
    "rf_model.fit(X_train, y_train)\n",
    "# 预测图片\n",
    "img = Image.open('D:/数字4.png')\n",
    "img = img.resize((32, 32)).convert('L')\n",
    "img_binary = img.point(lambda x: 0 if x > 128 else 1)\n",
    "arr = np.array(img_binary).reshape(1, -1)\n",
    "predicted_number = rf_model.predict(arr)[0]\n",
    "print(f\"随机森林预测结果: 数字 {predicted_number}\")\n",
    "plt.imshow(arr.reshape(32, 32), cmap='gray')\n",
    "plt.title(f'Random Forest Predicted: {predicted_number}')\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "883d9cdb-932b-428f-b5ab-6788cb25def5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑回归预测结果: 数字 4\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAGZCAYAAABmNy2oAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAY8klEQVR4nO3ce3DNd/7H8dchyUkkEUEupCSqFKturUtQBCkrMWixU1NLN9uLu5F23Zag2e2ybGOMNKtabHVa29mty2hpWdfR7urYMruptWbp6iw2REhDdiX5/P7w8/6JJOT4hePyfMzkj3zzPd/zOd9v4nm+n+/x9TjnnAAAkFTL3wMAANw9iAIAwBAFAIAhCgAAQxQAAIYoAAAMUQAAGKIAADBEAQBgHugorF69Wh6PR19++eUdfd4+ffqoT58+Pj0mNzdX8+bN0/Hjxyv8bOzYsUpISKiRsc2bN08ej8e+AgMD1bRpU73wwgs6depUjTzHvaAm96mvrt3/Ho9HERER6tOnjzZv3nxHnv/q78C1EhISNHbsWJ+2c/HiRc2bN087d+6sucH9r507d8rj8dTYtp977jl5PB6lpqbWyPbuZQH+HsCDKDs72+fH5Obmav78+erTp0+Ff6zmzJmjKVOm1NDortiyZYsiIiL03Xff6dNPP9WSJUu0b98+ffXVVwoMDKzR57ob3Y596ovhw4crPT1dZWVl+sc//qHMzEwNH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9etf/1oDBgzQ1q1b1a9fP0lXpqKefvpp9erVS+vWrVNJSYkWL15cITBXrV+/Xnv27NHcuXMVGxur6Oho5ebmqnv37mratKmWLFmi2NhYbd26VZMnT9aZM2eUkZEhSVq0aJHmzZunn/70p+rVq5cuX76sw4cPl/uHe/To0Tpw4IB+9rOfqWXLliooKNCBAwd09uzZKl+/c05Dhw7V9u3bNXPmTD355JM6dOiQMjIy9Pnnn+vzzz+X1+u19Q8ePKj09HTNmDFDMTExWrlypdLS0vTII4+oV69ePu//c+fO6ezZs2rRokW55TNnzlRiYqJycnJUq1YtRUdHV/u4bN++XUOGDFFiYqI++OADlZaWatGiRVUel2sVFhaqZ8+eOn78uKZPn66uXbvqu+++0+7du3Xy5El1795dW7Zs0cCBA5WWlqYf//jHkmShqO7xPH36tHr37q3AwEBlZ2crJiZG7733niZOnFhhTDt37lRSUpIyMjI0b968m76Gbdu26Te/+Y3279+v2rVr33T9B4Z7gK1atcpJcvv3769ynW7durno6GhXWFhoy0pKSlzbtm3dQw895MrKypxzzo0YMcKFhoa6vLw8W6+0tNS1adPGSXLHjh2z5b1793a9e/e27ydOnOjq1at3w7F++OGHTpLbsWNHhZ+NGTPGxcfH2/e7d+92ktzs2bNvuM3KZGRkOEnu1KlT7vLly+7cuXPut7/9rQsNDXXPPvusrVdUVOTq16/vBg8eXO7xpaWlrn379q5Lly62rHPnzq5JkybuP//5jy0rLCx0DRo0cNf/CkpyERERLj8/v9zyAQMGuIceesidP3++3PKJEye64OBgWz81NdV16NDhhq8xLCzMTZ069YbrXL9Pt2zZ4iS5RYsWlVtv3bp1TpJbsWKFLYuPj3fBwcHum2++sWWXLl1y9evXdy+99NINn9e5K/tg/Pjx7vLly+6///2v+/rrr933v/99J8ktX77cOefcjh07nCTXq1evco/15bh07drVNW7c2F26dMmWXbhwwdWvX7/CcYmPj3djxoyx7xcsWOAkuc8++6zK15GXl+ckuYyMjAo/q+7xnD59uvN4PO6rr74qt15ycnKFv4edO3e62rVru/nz51c5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+Ph4n8YdFRWl0tJSnTp1qtKgVUf79u3ttSUnJ2vAgAFasWKF0tLS1LlzZ/vZsmXL7FMy14uJibFPxPiyb66/+BwZGWlnZ1Wd/Vz9hyMgIEDTpk3TtGnTVFBQoG3btmnWrFkaMGCATpw4YRcrs7KylJWVpX/+85/auHGjZsyYoX//+9/asmVLpdtv0KCBSkpKlJeXVy4MzjmdOnVKnTt3rvRxtyoqKsoidiPX7ytfj0tlx6A6v7NRUVH69ttvb7peZXw5ng0aNLjlMVYlNzdXzjkNGzasws9OnDihyMhIvfHGG5o6deotP8c9zb+zV/5VnWsKiYmJLjY21l28eNGWlZaWuscee6zGrilUZv369U6S27x5s3POuY0bNzpJ7uOPP66wblXXFObMmXPD56jM1WsK174O55w7cuSICwgIcE899ZRz7sqcbL169dy4ceNuuk1frylMmDChwjb69+/v2rdvX24b1ZWVleUkub/+9a9VrjN06FAXFRVl31+/T7du3eokuV/96lflHnf1Ws9bb71ly6qam67OcXeu6n1wravXFD788MNyy305LjVxTWH79u1Vbv/ChQtOkvvJT35S4WfVPZ6+XFOorpMnT7odO3ZU+IqJiXHdunVzO3bscCdOnPB5u/cLzhR0Zb64sk/lDBo0SK+//rqSk5OVlJSkV155RUFBQcrOztZf/vIXvf/++/ZObfbs2dq0aZP69eun2bNnKyQkRDk5OSoqKpJ05fpCVV544QWFhISoR48eatSokU6dOqXXX39dERER9g60bdu2kqQVK1YoPDxcwcHBatasWaXTFE8++aRGjx6tzMxMnT59WqmpqfJ6vfrzn/+sOnXqaNKkST7voxYtWujFF19Udna29u7dq549e2rZsmUaM2aM8vPzNXz4cEVHRysvL08HDx5UXl6e3nzzTUnSggULlJKSogEDBmjKlCkqLS3VL3/5S4WFhSk/P79az7906VL17NlTTz75pMaNG6eEhAQVFhbq6NGj2rRpk835Dx48WG3bttUTTzxhH+HMyspSfHy8WrRoofPnzyspKUmjRo1Sq1atFB4erv3799snpKpy9Wxp+vTpunDhgnr06GGfPurYsaNGjx7t8z69HcLCwqp9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BAGCIAgDAcO8j4C7E/YzgL5wpAAAMUQAAGKIAADBEAQBgiAIAwBAFAIAhCgAAQxQAAIYoAAAMUQAAGKIAADBEAQBgiAIAwBAFAIAhCgAAQxQAAIYoAAAMUQAAGKIAADBEAQBgiAIAwBAFAIAJqO6KzrnbOQ4AwF2AMwUAgCEKAABDFAAAhigAAAxRAAAYogAAMEQBAGCIAgDAEAUAgPkfGIWGVk1HciUAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#4.逻辑回归算法\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "df = pd.read_excel('D:/手写字体识别.xlsx')\n",
    "X = df.drop(columns='对应数字')\n",
    "y = df['对应数字']\n",
    "# 划分训练测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "# 逻辑回归模型训练（使用多分类）\n",
    "lr_model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)\n",
    "lr_model.fit(X_train, y_train)\n",
    "# 预测图片\n",
    "img = Image.open('D:/数字4.png')\n",
    "img = img.resize((32, 32)).convert('L')\n",
    "img_binary = img.point(lambda x: 0 if x > 128 else 1)\n",
    "arr = np.array(img_binary).reshape(1, -1)\n",
    "predicted_number = lr_model.predict(arr)[0]\n",
    "print(f\"逻辑回归预测结果: 数字 {predicted_number}\")\n",
    "plt.imshow(arr.reshape(32, 32), cmap='gray')\n",
    "plt.title(f'Logistic Regression Predicted: {predicted_number}')\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89e7fd7b-79c9-4678-bd04-5cdde97149d9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
